﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
//0712455
namespace DAO
{
    public class GiaoDichChuyenTienDenTKCungNHNoDAO
    {
        public static int ThemGiaoDich(GiaoDichDTO gdDto, PhieuChuyenTienDenTKCungNHDTO ctCungNHDto, string recentUser, ref double phiGD)
        {
            SqlConnection cnn = KetNoiCSDL.TaoKetNoi();
            SqlCommand cmd = new SqlCommand("sp_ChuyenTienCungNH", cnn);

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@MaTKChuyen", SqlDbType.Char);
            cmd.Parameters.Add("@MaTKNhan", SqlDbType.Char);
            cmd.Parameters.Add("@ChiNhanhNhan", SqlDbType.Int);
            cmd.Parameters.Add("@SoTien", SqlDbType.Money);
            cmd.Parameters.Add("@MaNVGD", SqlDbType.Char);
            cmd.Parameters.Add("@ChiNhanhGD", SqlDbType.Int);
            cmd.Parameters.Add("@PhiGD", SqlDbType.Money);
            cmd.Parameters["@PhiGD"].Direction = ParameterDirection.Output;

            cmd.Parameters["@MaTKChuyen"].Value = gdDto.MaTK;
            cmd.Parameters["@MaTKNhan"].Value = ctCungNHDto.MaTKNhan;
            cmd.Parameters["@ChiNhanhNhan"].Value = ctCungNHDto.MaChiNhanh;
            cmd.Parameters["@SoTien"].Value = ctCungNHDto.SoTien;
            cmd.Parameters["@MaNVGD"].Value = recentUser;
            cmd.Parameters["@ChiNhanhGD"].Value = NhanVienDAO.LayChiNhanhGD(recentUser);

            cnn.Open();
            int rs = 0;
            try
            {
                rs = (int)cmd.ExecuteNonQuery();
                phiGD = Double.Parse(cmd.Parameters["@PhiGD"].Value.ToString());
                cnn.Close();
                return rs;
            }
            catch (Exception ex)
            {
                if (cnn.State == ConnectionState.Open)
                    cnn.Close();
                throw ex;
            }
        }
    }
}
